<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="IP地址" prop="hostname">
        <el-input @input="handleQuery" v-model="queryParams.hostname" placeholder="请输入IP地址" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="显示名称" prop="display">
        <el-input @input="handleQuery" v-model="queryParams.display" placeholder="请输入显示名称" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="是否启用snmp，0为启用，其他为禁用" prop="snmpDisable">
        <el-select @change="handleQuery" v-model="queryParams.snmpDisable" placeholder="请选择是否启用snmp，0为启用，其他为禁用"
          clearable class="!w-240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['nms:devices:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['nms:devices:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['nms:devices:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="device_id" align="center" prop="deviceId" />
      <el-table-column label="插入时间" align="center" prop="inserted" :formatter="dateFormatter" width="180px" />
      <el-table-column label="IP地址" align="center" prop="hostname" />
      <el-table-column label="系统名称" align="center" prop="sysName" />
      <el-table-column label="显示名称" align="center" prop="display" />
      <el-table-column label="ip" align="center" prop="ip" />
      <el-table-column label="overwrite_ip" align="center" prop="overwriteIp" />
      <el-table-column label="SNMP社区字符串" align="center" prop="community" />
      <el-table-column label="认证级别" align="center" prop="authlevel" />
      <el-table-column label="authname" align="center" prop="authname" />
      <el-table-column label="authpass" align="center" prop="authpass" />
      <el-table-column label="认证算法" align="center" prop="authalgo" />
      <el-table-column label="加密密码" align="center" prop="cryptopass" />
      <el-table-column label="加密算法" align="center" prop="cryptoalgo" />
      <el-table-column label="SNMP版本" align="center" prop="snmpver" />
      <el-table-column label="port" align="center" prop="port" />
      <el-table-column label="传输协议" align="center" prop="transport" />
      <el-table-column label="timeout" align="center" prop="timeout" />
      <el-table-column label="重试次数" align="center" prop="retries" />
      <el-table-column label="是否启用snmp，0为启用，其他为禁用" align="center" prop="snmpDisable" />
      <el-table-column label="BGP本地自治系统号" align="center" prop="bgpLocalAs" />
      <el-table-column label="sysObjectID" align="center" prop="sysObjectID" />
      <el-table-column label="系统描述" align="center" prop="sysDescr" />
      <el-table-column label="默认系统联系人" align="center" prop="sysContact" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="硬件" align="center" prop="hardware" />
      <el-table-column label="特征" align="center" prop="features" />
      <el-table-column label="操作系统" align="center" prop="locationId" />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="状态，1为在线，其他为离线" align="center" prop="status" />
      <el-table-column label="status_reason" align="center" prop="statusReason" />
      <el-table-column label="ignore" align="center" prop="ignore" />
      <el-table-column label="0为不禁止，其他为禁止" align="center" prop="disabled" />
      <el-table-column label="系统运行时间" align="center" prop="uptime" />
      <el-table-column label="代理运行时间" align="center" prop="agentUptime" />
      <el-table-column label="最后轮询时间" align="center" prop="lastPolled" :formatter="dateFormatter" width="180px" />
      <el-table-column label="最后轮询尝试时间" align="center" prop="lastPollAttempted" :formatter="dateFormatter"
        width="180px" />
      <el-table-column label="最后轮询耗时" align="center" prop="lastPolledTimetaken" />
      <el-table-column label="最后发现耗时" align="center" prop="lastDiscoveredTimetaken" />
      <el-table-column label="最后发现时间" align="center" prop="lastDiscovered" :formatter="dateFormatter" width="180px" />
      <el-table-column label="最后Ping时间" align="center" prop="lastPing" :formatter="dateFormatter" width="180px" />
      <el-table-column label="最后Ping耗时" align="center" prop="lastPingTimetaken" />
      <el-table-column label="用途" align="center" prop="purpose" />
      <el-table-column label="type" align="center" prop="type" />
      <el-table-column label="序列号" align="center" prop="serial" />
      <el-table-column label="icon" align="center" prop="icon" />
      <el-table-column label="轮询组" align="center" prop="pollerGroup" />
      <el-table-column label="是否覆盖系统位置" align="center" prop="overrideSyslocation" />
      <el-table-column label="notes" align="center" prop="notes" />
      <el-table-column label="端口关联模式" align="center" prop="portAssociationMode" />
      <el-table-column label="最大深度" align="center" prop="maxDepth" />
      <el-table-column label="是否禁用通知" align="center" prop="disableNotify" />
      <el-table-column label="是否忽略状态" align="center" prop="ignoreStatus" />
      <el-table-column label="quyu" align="center" prop="quyu" />
      <el-table-column label="子区域" align="center" prop="quyusub" />
      <el-table-column label="tag1" align="center" prop="tag1" />
      <el-table-column label="tag2" align="center" prop="tag2" />
      <el-table-column label="tag3" align="center" prop="tag3" />
      <el-table-column label="tag4" align="center" prop="tag4" />
      <el-table-column label="tag5" align="center" prop="tag5" />
      <el-table-column label="tag6" align="center" prop="tag6" />
      <el-table-column label="tag7" align="center" prop="tag7" />
      <el-table-column label="tag8" align="center" prop="tag8" />
      <el-table-column label="tag9" align="center" prop="tag9" />
      <el-table-column label="tag10" align="center" prop="tag10" />
      <el-table-column label="tag11" align="center" prop="tag11" />
      <el-table-column label="tag12" align="center" prop="tag12" />
      <el-table-column label="tag13" align="center" prop="tag13" />
      <el-table-column label="xianlu" align="center" prop="xianlu" />
      <el-table-column label="SNMP启用状态" align="center" prop="snmpyes" />
      <el-table-column label="类型名称" align="center" prop="typeName" />
      <el-table-column label="拓扑图x" align="center" prop="x" />
      <el-table-column label="拓扑图y" align="center" prop="y" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)" v-hasPermi="['nms:devices:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['nms:devices:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DevicesForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DevicesApi, Devices } from '@/api/business/devices'
import DevicesForm from './DevicesForm.vue'

/** 设备 列表 */
defineOptions({ name: 'Devices' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Devices[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  hostname: undefined,
  display: undefined,
  snmpDisable: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DevicesApi.getDevicesPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DevicesApi.deleteDevices(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除设备 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await DevicesApi.deleteDevicesList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: Devices[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DevicesApi.exportDevices(queryParams)
    download.excel(data, '设备.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
